package com.chixing.day16_io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
    /*public static void main(String[] args) {
        //1.创建字节读取流 InputStream
        InputStream is = null;
        try {
            is = new FileInputStream("f:/test1/hello.txt");
            String line;
            byte[] data = new byte[1024];
            int readCount = 0;
            // 这个方法的返回值是：读取到的字节数量。（不是字节本身）;1个字节都没有读取到返回-1(文件读到末尾)
            while((readCount = is.read(data)) != -1) {
                // 不应该全部都转换，应该是读取了多少个字节，转换多少个。
                System.out.print(new String(data, 0, readCount));
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {//3.关闭流
                is.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }*/
    public static void main(String[] args) throws IOException {
        InputStream is = null;
        try {
            is = new FileInputStream("f:/test1/hello.txt");
            byte[] bytes = new byte[1024];
            int size = 0;


            int temp;
            while ((temp=is.read()) != -1){//说明还有下一个字节
                bytes[size++] = (byte) temp;// int --->byte
            }

            String result = new String(bytes);
            System.out.println(result);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }finally {////3. 关闭流
            is.close();
        }
    }

}
